package com.zoo.diannaopeijian.service.impl;

import com.github.pagehelper.PageHelper;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.domain.WareExample;
import com.zoo.diannaopeijian.mapper.WareMapper;
import com.zoo.diannaopeijian.service.WareService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zx583 on 2018-01-10.
 */
@Service
public class WareServiceImpl implements WareService {
    @Resource
    private WareMapper wareMapper;

    @Override
    public List<Ware> getWare(Ware ware) {
        WareExample example = new WareExample();

        if (ware != null) {
            WareExample.Criteria criteria = example.createCriteria();

            // Integer wareid
            if (ware.getWareid() != null) {
                criteria.andWareidEqualTo(ware.getWareid());
            }

            // String warename
            if (!StringUtils.isEmpty(ware.getWarename())) {
                criteria.andWarenameEqualTo(ware.getWarename());
            }

            // String waredescribe
            if (!StringUtils.isEmpty(ware.getWaredescribe())) {
                criteria.andWaredescribeEqualTo(ware.getWaredescribe());
            }

            // String barcode
            if (!StringUtils.isEmpty(ware.getBarcode())) {
                criteria.andBarcodeEqualTo(ware.getBarcode());
            }
        }

        return wareMapper.selectByExample(example);
    }

    @Override
    public List<Ware> getWareWithPage(Integer page, Integer rows, Ware ware) {
        PageHelper.startPage(page, rows);

        return getWare(ware);
    }

    @Override
    public List<Ware> getFuzzyWare(Ware ware) {
        return wareMapper.selectFuzzyWare(ware);
    }

    @Override
    public List<Ware> getFuzzyWareWithPage(Integer page, Integer rows, Ware ware) {
        PageHelper.startPage(page, rows);

        return getFuzzyWare(ware);
    }

    @Override
    public boolean addWare(Ware ware) {
        return wareMapper.insertSelective(ware) == 1;
    }

    @Override
    public boolean deleteWare(Integer wareid) {
        return wareMapper.deleteByPrimaryKey(wareid) == 1;
    }

    @Override
    public boolean updateWare(Ware ware) {
        return wareMapper.updateByPrimaryKeySelective(ware) == 1;
    }

    @Override
    public List<Ware> getWareList(Ware ware) {
        return wareMapper.selectFuzzyOrWare(ware);
    }
}
